﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.ZXBuiness;
using Repository.ZXBuiness;
using Repository.Util;
using Model.Util;
using Model.Finance;
using Model.Const;

namespace Services.ZXBuiness
{
   public  class ZXTiXianServices : Service
    {
        IRepositoryAsync<ZXUserFenRun> m_ZXUserFenRunRepository;
        IRepositoryAsync<ZXTiXianApply> m_ZXTiXianApplyRepository;
        IRepositoryAsync<ZXOrderFenRunInfo> m_ZXOrderFenRunInfoRepository;
        IRepositoryAsync<Identity> m_IdentityRepository;
        IRepositoryAsync<AccountBalance> m_AccountBalanceRepository;
        IRepositoryAsync<ZXUserInfo> m_ZXUserInfoRepository;
        public ZXTiXianServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_ZXUserFenRunRepository = m_UnitOfWork.RepositoryAsync<ZXUserFenRun>();
            m_ZXTiXianApplyRepository = m_UnitOfWork.RepositoryAsync<ZXTiXianApply>();
            m_ZXOrderFenRunInfoRepository = m_UnitOfWork.RepositoryAsync<ZXOrderFenRunInfo>();
            m_IdentityRepository = m_UnitOfWork.RepositoryAsync<Identity>();
            m_AccountBalanceRepository = m_UnitOfWork.RepositoryAsync<AccountBalance>();
            m_ZXUserInfoRepository = m_UnitOfWork.RepositoryAsync<ZXUserInfo>();
        }
    


        public List<ZXTiXianApplyModel> GetTXapplyList( int offset, int limit,    string ApplyUserCode, out int total)
        {
            total = 0;
            List<ZXTiXianApplyModel> list = m_ZXTiXianApplyRepository.GetTiXianApplyList(  offset, limit, ApplyUserCode);
            if (list != null && list.Count > 0)
            {
                total = list.FirstOrDefault().total;
            }            
            return list;
        }

        public ZXTiXianApply GetZXTiXianApplyById(string tixianId)
        {
            return m_ZXTiXianApplyRepository.GetById(tixianId);
        }

        public int RejectThispply(string tixianId, string oper, string remarks)
        {
            ZXTiXianApply tx = GetZXTiXianApplyById(tixianId);
            if (tx == null)
            {
                return 2;//提现记录不存在
            }
            ZXUserFenRun zfr = m_ZXUserFenRunRepository.GetById(tx.UserFenRunId);
            m_UnitOfWork.BeginTransaction();
            zfr.IsTiXian = 4;
            zfr.Remarks = remarks;
            zfr.EntityState = EntityState.Modified;

            tx.EntityState = EntityState.Modified;
            tx.State = 10;
            tx.Remarks = remarks;
            m_ZXTiXianApplyRepository.Update(tx);
            m_ZXUserFenRunRepository.Update(zfr);
            if (m_UnitOfWork.SaveChanges() == 2)
            {
                m_UnitOfWork.Commit();
                return 1;
            }
            else {
                m_UnitOfWork.Rollback();
            }
            return 0;
        }

        public int ApproveThisApply(string tixianId,string oper,string remarks)
        {
            ZXTiXianApply tx = GetZXTiXianApplyById(tixianId);
            if (tx == null)
            {
                return 2;//提现记录不存在
            }
            ZXUserInfo ZU = m_ZXUserInfoRepository.GetById(tx.ZX_UserId);
            string BalanceCode =CreateFRHZCode("FRHZ", tixianId);
            ZXUserFenRun zfr = m_ZXUserFenRunRepository.GetById(tx.UserFenRunId);
            m_UnitOfWork.BeginTransaction();

            tx.ApproveTime = DateTime.Now;
            tx.ApproveOper = oper;
            tx.ApproveMoney = zfr.FenRunMoney;
            tx.EntityState = EntityState.Modified;
            tx.BalanceCode = BalanceCode;
            tx.State = 5;
            m_ZXTiXianApplyRepository.Update(tx);
            zfr.AccountBalanceCode = BalanceCode;
            zfr.IsTiXian = 10;
            zfr.AccountBalanceCode = BalanceCode;
            zfr.EntityState = EntityState.Modified;
            m_ZXUserFenRunRepository.Update(zfr);
            //插入流水表
            //加上流水表信息
            AccountBalance balance = new AccountBalance();
            balance.BalanceMoney = zfr.FenRunMoney;// 
            balance.BuinessCode = (int)EnumAccountBalance_BuinessCode.提成;
            balance.CreateTime = DateTime.Now;
            balance.EntityState = EntityState.Added;
            balance.OrgaCode = (int)EnumAccountBalance_OrgaCode.线下支付;
            balance.RelationId = tixianId;
            balance.UserId = ZU.WX_UserId;
            balance.AccountBalanceCode = BalanceCode;
            ZU.HaveEarnMoney = ZU.HaveEarnMoney + zfr.FenRunMoney;
            ZU.EntityState = EntityState.Modified;
            m_ZXUserInfoRepository.Update(ZU);
            m_AccountBalanceRepository.Insert(balance);
            if (m_UnitOfWork.SaveChanges() == 4)
            {
                m_UnitOfWork.Commit();
                return 1;
            }
            else
            {
                m_UnitOfWork.Rollback();
                return 0;
            }


        }
        /// <summary>
        /// 创建提现核准业务码
        /// </summary>
        /// <param name="pre"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public string CreateFRHZCode(string pre, string projectId)
        {
            Identity iden = new Identity();
            iden.CreateTime = DateTime.Now;
            iden.EntityState = EntityState.Added;
            iden.ProjectID = projectId;
            iden.Guid = Guid.NewGuid().ToString();
            m_IdentityRepository.Insert(iden);
            m_UnitOfWork.SaveChanges();
            Identity nid = m_IdentityRepository.Get(iden.Guid);
            string year = DateTime.Now.Year.ToString().Substring(2, 2);
            string Monty = DateTime.Now.Month.ToString();
            string day = DateTime.Now.Day.ToString();

            return pre + year + Monty + day + nid.ID.ToString();
        }

    }
}
